package org.soarsky.wealthchengdu.module.info;

import java.util.ArrayList;
import java.util.List;

import org.soarsky.wealthchengdu.R;
import org.soarsky.wealthchengdu.appliction.MyAppliction;
import org.soarsky.wealthchengdu.entity.JsonResult;
import org.soarsky.wealthchengdu.entity.LogonResult;
import org.soarsky.wealthchengdu.entity.NewsData;
import org.soarsky.wealthchengdu.entity.NewsResultMsg;
import org.soarsky.wealthchengdu.network.ParserResults;
import org.soarsky.wealthchengdu.util.Config;
import org.soarsky.wealthchengdu.view.ALLSendDialog;
import org.soarsky.wealthchengdu.view.AddView;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

/**
 * 成都镜相等 有九宫格图片的界面
 * 
 * @author huang
 * 
 */
public class MirrorchengduDataShow extends AddView {

	public static MirrorChengdu_adapter mAdapter;
	private static MirrorchengduDataShow mirrorchengduDataShow = null;
	public static List<InforImageBean> inforImages = new ArrayList<InforImageBean>();
	private static ParserResults service;

	private static List<NewsData> listdata;

	private MirrorchengduDataShow(Activity activity) {
		super(activity);
	}

	/**
	 * 获取addview实例
	 * 
	 * @param context
	 * @return
	 */
	public static MirrorchengduDataShow getinstace(Activity activity) {
		if (mirrorchengduDataShow == null) {
			mirrorchengduDataShow = new MirrorchengduDataShow(activity);
			mAdapter = new MirrorChengdu_adapter(activity);
			service = new ParserResults();
			listdata = new ArrayList<NewsData>();
		}
		return mirrorchengduDataShow;
	}

	/**
	 * 获取控件
	 * 
	 * @param view
	 */
	public View getView() {
		GridView gridView;
		View view = getinfalntview(R.layout.activity_mirror);
		gridView = (GridView) view.findViewById(R.id.activity_mirror_gridview);
		gridView.setAdapter(mAdapter);
		if (listdata.size() > 0) {
			listdata.clear();
		}
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(activity,
						MirrorBigImageActivity.class);
				MyAppliction.ID = listdata.get(arg2).getId();
				if(MyAppliction.mCn==0)
				{
					MyAppliction.galleryname = listdata.get(arg2).getTitle();
				}else{
					MyAppliction.galleryname = listdata.get(arg2).getTitleEn();
				}
				activity.startActivity(intent);
			}
		});
		loadDatas();
		return view;
	}

	private static void loadDatas() {
		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... params) {
				// TODO Auto-generated method stub
				getLogon();
				try {
					JsonResult<NewsResultMsg> focus = service.getNews("105",
							"2");
					if (focus == null) {
						return false;
					}
					if (focus.isError()) {
						error(focus);
						// 这里做 数据错误处理
						return false;
					} else {
						NewsResultMsg fdata = focus.getData();
						List<NewsData> data = fdata.getData();
						if (data.size() > 0) {
							for (NewsData newsdata : data) {
								listdata.add(newsdata);
								Log.e("ID", newsdata.getId());
							}
						}
						return true;
					}

				} catch (Exception e) {
					// TODO Auto-generated catch block
				}
				return false;
			}

			protected void onPostExecute(Boolean result) {
				// UI更新操作
				if (result) {
					setList();
				} else {
					Toast.makeText(
							activity,
							Config.changeLanguage(MyAppliction.mCn).getNodata(),
							Toast.LENGTH_LONG).show();
				}
				ALLSendDialog.dimssdialog();
			};
		}.execute();
	}

	// 必须先登录
	private static void getLogon() {
		try {
			JsonResult<LogonResult> jsonResult = service.authLogon("张三");
			if (jsonResult != null) {
				if (jsonResult.isError()) {
					error(jsonResult);
				} else {// 登录成功
					System.out.println(jsonResult.getData().getUserId() + "==="
							+ jsonResult.getData().getNickName());
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.toString() + "===");
		}
	}

	private static void error(JsonResult<?> jsonResult) {
		System.out.println("错误码:" + jsonResult.getCode() + "===错误信息:"
				+ jsonResult.getMsg());
	}

	/**
	 * 更改数据
	 */
	public static void setList() {
		if (listdata != null && listdata.size() > 0) {
			mAdapter.setList(listdata);
		}
	}
}
